package com.xxl.job.common.config;

import com.xxl.job.common.utils.StringUtils;
import com.xxl.job.common.utils.YamlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

/**
 * 加载 application文件
 * 注意：按照 application- 后缀进行获取配置信息
 *
 * @author zyc
 * @date: 2020/1/1
 * @version: 1.0
 */
public class ApplicationSystemYlmLoad {
    private static final Logger log = LoggerFactory.getLogger(ApplicationSystemYlmLoad.class);

    private static String NAME = "application" + "-" + ProfileManager.getActiveProfile() + ".yml";

    /**
     * 当前对象实例
     */
    private static ApplicationSystemYlmLoad systemylmload;

    /**
     * 保存全局属性值
     */
    private static Map<String, String> map = new HashMap<String, String>();

    private ApplicationSystemYlmLoad() {
    }

    /**
     * 静态工厂方法
     */
    public static synchronized ApplicationSystemYlmLoad getInstance() {
        if (systemylmload == null) {
            systemylmload = new ApplicationSystemYlmLoad();
        }
        return systemylmload;
    }

    /**
     * 获取配置
     */
    public static String getConfig(String key) {
        String value = map.get(key);
        if (value == null) {
            Map<?, ?> yamlMap = null;
            try {
                yamlMap = YamlUtil.loadYaml(NAME);
                value = String.valueOf(YamlUtil.getProperty(yamlMap, key));
                map.put(key, value != null ? value : StringUtils.EMPTY);
            } catch (FileNotFoundException e) {
                log.error("获取全局配置异常 {}", key);
            }
        }
        return value;
    }

    /**
     * 获取国家时区配置
     */
    public static String getNationalTimeZone() {
        return getConfig("country.timeZone");
    }
}
